package com.cloudsubassemblykafka10024.partitioner;

import org.apache.kafka.clients.producer.KafkaProducer;
import org.apache.kafka.clients.producer.ProducerConfig;
import org.apache.kafka.clients.producer.ProducerRecord;
import org.apache.kafka.common.serialization.StringSerializer;

import java.util.Properties;

/**
 * @Description: TODO
 * @Author zx
 * @Date 2024/12/24 20:31
 * @Version 1.0
 */
public class KafkaProducerPartitioner {
    public static void main(String[] args) {
        // 创建 Kafka Producer
        Properties properties = new Properties();
        properties.put(ProducerConfig.BOOTSTRAP_SERVERS_CONFIG, "192.168.18.220:9092");
        properties.put(ProducerConfig.KEY_SERIALIZER_CLASS_CONFIG, StringSerializer.class.getName());
        properties.put(ProducerConfig.VALUE_SERIALIZER_CLASS_CONFIG, StringSerializer.class.getName());
        properties.put(ProducerConfig.PARTITIONER_CLASS_CONFIG, UserDefinePartitioner.class.getName());

        KafkaProducer<String, String> kafkaProducer = new KafkaProducer<>(properties);

        // 发送消息
        for (int i = 0; i < 100; i++) {
            ProducerRecord<String, String> producerRecord = new ProducerRecord<>("messageDML001", "key" + i, "value" + i);
            // ProducerRecord<String, String> producerRecord = new ProducerRecord<>("messageDML001", "value" + i);
            kafkaProducer.send(producerRecord);
        }

        // 关闭资源
        kafkaProducer.close();
    }
}
